/**
 * Created by yongwang on 2015/6/15.
 */
var toolUtil = require('../../common/utils/toolUtil');
var async = require('async');
var mysqlUtil = require('../../common/utils/mysqlUtil');
var communityService = require('../communityService');
var Users = mongoose.model('user');

exports.getScoreShopList = function (page,callback) {
    var offset = (Number(page) - 1) * 10,
        sql = "SELECT * FROM score_shop limit "+offset+",10";

    async.series([
        function (callbackInner) {
            mysqlUtil.query(sql, function (err, result) {
                callbackInner(err, result);
            });
        },
        function (callbackInner) {
            var sql = 'SELECT COUNT(*) as pages FROM score_shop';
            mysqlUtil.query(sql, function (err, result) {
                callbackInner(err, result);
            });
        }
    ], callback);
}

exports.changeStatus = function (map,callback) {
    var sql = "update score_shop set status = "+map.status+" WHERE id = " +map.id;
    mysqlUtil.query(sql, function (err, result) {
        callback(err, result);
    });
}

exports.searchGoods = function (name,callback) {
    var sql = "SELECT * FROM score_shop where product_name like '%"+name+"%'";
    mysqlUtil.query(sql, function (err, result) {
        callback(err, result);
    });
}

exports.searchValue = function (map,callback) {
    Users.find({
        community: map.communityName,
        nickName: new RegExp(map.userName)
    }, 'nickName mobilePhone roomUnit roomNumber community').populate('community', 'communityName').exec(function (err, user) {
        logger.info(user);
        var i= 0,length = user.length,data = new Array(length);
        user.forEach(function(item){
            var sql = 'SELECT total_value,current_value FROM score_info WHERE user_id=?';
            mysqlUtil.query(sql, [item._id+""], function (err, result) {
                data[i] = item;
                if (result != null && result.length > 0) {
                    data[i]._doc.totalValue = result[0].total_value;
                    data[i]._doc.currentValue = result[0].current_value;
                }
                logger.info(i);
                i++;
                if(length == i){logger.info(data);callback(err, data);}
            });
        });
    })
}

exports.getCommunityScoreList = function(map,callback){

    Users.find({
        community: map.communityName
    }, 'nickName mobilePhone roomUnit roomNumber community').populate('community', 'communityName').limit(10).skip(Number((map.page - 1)*10)).exec(function (err, user) {
        logger.info(user);
        var i= 0,length = user.length,data = new Array(length),data1 = {};
        //user.forEach(function(item){
        //    var sql = 'SELECT total_value,current_value FROM score_info WHERE user_id=?';
        //    mysqlUtil.query(sql, [item._id+""], function (err, result) {
        //        data[i] = item;
        //        if (result != null && result.length > 0) {
        //            data[i]._doc.totalValue = result[0].total_value;
        //            data[i]._doc.currentValue = result[0].current_value;
        //        }
        //        logger.info(i);
        //        i++;
        //        if(length == i){
        //            Users.find({
        //                community: map.communityName
        //            }).count().exec(function(err, count){
        //                logger.info("count "+count);
        //                data1 = {
        //                    data: data,
        //                    pages: count
        //                }
        //                logger.info(data1);callback(err, data1);
        //            });
        //        }
        //    });
        //});

        async.each(user,function(item,callbackInner){
            mysqlUtil.query('SELECT total_value,current_value FROM score_info WHERE user_id=?', [item._id+""], function (err, result) {
                logger.info(item);
                data[i] = item;
                if (result != null && result.length > 0) {
                    data[i]._doc.totalValue = result[0].total_value;
                    data[i]._doc.currentValue = result[0].current_value;
                }
                i++;
                if(length == i){
                    Users.find({
                        community: map.communityName
                    }).count().exec(function(err, count){
                        logger.info("count "+count);
                        data1 = {
                            data: data,
                            pages: count
                        }
                        logger.info(data1);callback(err, data1);
                    });
                }
                //callbackInner()
            });
        },function(err){
            //data
        })
    })
}

exports.searchExchangeGoods = function (name,callback) {
    //var sql = "SELECT * FROM score_exchange_record where product_name like '%"+name+"%'";
    var sql = "SELECT score_shop.product_name,score_exchange_record.exchange_num,score_exchange_record.product_price,score_exchange_record.user_name,score_exchange_record.mobile,score_exchange_record.address,score_exchange_record.status,score_exchange_record.update_time FROM score_shop,score_exchange_record WHERE score_shop.id = score_exchange_record.product_id and score_shop.product_name like '%"+name+"%'";
    mysqlUtil.query(sql, function (err, result) {
        callback(err, result);
    });
}

